package com.jianghu;

import java.util.ArrayList;
import java.util.HashMap;

import com.jianghu.helpers.GetLineFromtxt;
import com.jianghu.view.HeaderOfUserTrends;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class UserTrendsAuto extends Activity {

	private String user;
	private Thread mThread;
	public Boolean surplus = true;
	LinearLayout loadingLayout;
	ProgressBar progressBar;
	private LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.WRAP_CONTENT,
			LinearLayout.LayoutParams.WRAP_CONTENT);
	private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.FILL_PARENT,
			LinearLayout.LayoutParams.FILL_PARENT);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usertrends);
		Intent intent = getIntent();
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.HORIZONTAL);
		progressBar = new ProgressBar(this);
		progressBar.setPadding(0, 0, 15, 0);
		layout.addView(progressBar, mLayoutParams);
		TextView textView = new TextView(this);
		textView.setText("加载中...");
		textView.setGravity(Gravity.CENTER_VERTICAL);
		layout.addView(textView, FFlayoutParams);
		layout.setGravity(Gravity.CENTER);
		loadingLayout = new LinearLayout(this);
		loadingLayout.addView(layout, mLayoutParams);
		loadingLayout.setGravity(Gravity.CENTER);

		user = intent.getStringExtra("item").toString()
				+ this.getString(R.string.trends);
		TextView whoestrend = (TextView) findViewById(R.id.whosetrends);
		whoestrend.setText(user);

		ImageButton home = (ImageButton) findViewById(R.id.homebuttonofusertrends);
		home.setOnClickListener(new homeClickListener());

		ImageButton refresh = (ImageButton) findViewById(R.id.refreshbuttonofusertrends);
		refresh.setOnClickListener(new refreshClickListener());

		final ListView list = (ListView) findViewById(R.id.listViewOfUserTrends);
		list.addFooterView(loadingLayout);
		final ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();

		for (int i = 0; i < 20; i++) {

			HashMap<String, Object> map = new HashMap<String, Object>();
			String str = GetLineFromtxt.readAppointedLineNumber(this, i + 1);
			if (str == null) {
				surplus = false;
				break;
			}
			map.put("UserName", "设计工坊");
			map.put("timeoftrends", i + "小时前");
			map.put("contentsoftrends", str);
			listItem.add(map);
		}

		final SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,
				R.layout.listview_user_trends, new String[] { "UserName",
						"timeoftrends", "contentsoftrends" }, new int[] {
						R.id.UserNameofTrends, R.id.timeoftrends,
						R.id.contentsoftrends });
		HeaderOfUserTrends headerView = new HeaderOfUserTrends(this);
		headerView.setTextViewforFans("450");
		headerView.setTextViewforLikes("50");
		headerView.setTextViewforShares("10");
		headerView.setTextViewforFriends("150");

		list.addHeaderView(headerView, null, false);
		list.setAdapter(listItemAdapter);
		list.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, final int totalItemCount) {
				if (firstVisibleItem + visibleItemCount == totalItemCount)
					if (mThread == null || !mThread.isAlive()) {
						mThread = new Thread() {
							@Override
							public void run() {
								try {
									loadRemnantListItem(totalItemCount - 1);
									Thread.sleep(5000);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
								Message message = new Message();
								message.what = 1;
								handler.sendMessage(message);
							}
						};
						mThread.start();
					}

			}

			private Handler handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					switch (msg.what) {
					case 1:
						listItemAdapter.notifyDataSetChanged();
						if (surplus == false) {

							list.removeFooterView(loadingLayout);

						}

						break;
					default:
						break;
					}
				}
			};

			private void loadRemnantListItem(int totalItemCount) {

				int max = totalItemCount + 20;
				for (int i = totalItemCount; i < max; i++) {

					HashMap<String, Object> map = new HashMap<String, Object>();
					String str = GetLineFromtxt.readAppointedLineNumber(
							UserTrendsAuto.this, i + 1);
					if (str == null) {
						surplus = false;
						break;
					}
					map.put("UserName", "设计工坊");
					map.put("timeoftrends", i + "小时前");
					map.put("contentsoftrends", str);
					listItem.add(map);
				}

			}
		});

	}

	class homeClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(UserTrendsAuto.this, MainPage.class);
			UserTrendsAuto.this.startActivity(intent);
		}

	}

	class refreshClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {

		}

	}

}